package com.soft.servlets.emp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.soft.pojo.Result;
import com.soft.service.EmpService;
import com.soft.service.impl.EmpServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/deleteEmp")
public class DeleteEmpServlet extends HttpServlet {
    private EmpService service = new EmpServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = null;
        if (req.getParameter("empno") != null){
            int empno = Integer.parseInt(req.getParameter("empno"));
            int i = service.deleteEmp(empno);
            if (i > 0){
                result = Result.success();
            }else {
                result = Result.error("删除失败");
            }
        }else {
            result = Result.error("员工编号不存在");
        }
        //响应
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        resp.setContentType("application/json");
        resp.getWriter().print(json);
    }
}
